package me.send.broker.mapper.provider;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import me.send.broker.entity.BrokerWalletLog;

public class WalletProvider {
    public String chargeInDetails(Map<String, Object> param) {
	String sql = "select walletLogId id,changeDesc,changeTime,tipAmount total "
		     + "from broker_wallet_log where 1=1 ";
	if (param != null) {
	    int popid = (Integer) param.get("popid");
	    int size = (Integer) param.get("size");
	    int lastid = (Integer) param.get("lastId");
	    sql += "and brokerId=" + popid + " and tipAmount>0 ";
	    if (lastid != 0) {// 首页
		sql += "and walletLogId<" + lastid;
	    }
	    sql += " order by walletLogId desc limit " + size;
	}
	return sql;
    }
    
    public String chargeInPage(Map<String, Object> param) {
	String sql = "";
	if (param != null) {
	    int popid = (Integer) param.get("popid");
	    int size = (Integer) param.get("size");
	    sql += "select count(*)/" + size + "+1 "
		   + "from broker_wallet_log where 1=1 ";
	    sql += "and brokerId=" + popid + " and tipAmount>0 ";
	}
	return sql;
    }
    
    public String buyDetails(Map<String, Object> param) {
	String sql = "select walletLogId id,changeDesc,changeTime,purchaseAmount total "
		     + "from broker_wallet_log where 1=1 ";
	if (param != null) {
	    int popid = (Integer) param.get("popid");
	    int size = (Integer) param.get("size");
	    int lastid = (Integer) param.get("lastId");
	    sql += "and brokerId=" + popid + " and purchaseAmount>0 ";
	    if (lastid != 0) {// 首页
		sql += "and walletLogId<" + lastid;
	    }
	    sql += " order by walletLogId desc limit " + size;
	}
	return sql;
    }
    
    public String buyPage(Map<String, Object> param) {
	String sql = "";
	if (param != null) {
	    int popid = (Integer) param.get("popid");
	    int size = (Integer) param.get("size");
	    sql += "select count(*)/" + size + "+1 "
		   + "from broker_wallet_log where 1=1 ";
	    sql += "and brokerId=" + popid + " and purchaseAmount>0 ";
	}
	return sql;
    }
    
    public String allDetails(Map<String, Object> param) {
	String sql = "select walletLogId id,changeDesc,changeTime,purchaseAmount+tipAmount total "
		     + "from broker_wallet_log where 1=1 ";
	if (param != null) {
	    int popid = (Integer) param.get("popid");
	    int size = (Integer) param.get("size");
	    int lastid = (Integer) param.get("lastId");
	    sql += "and brokerId=" + popid;
	    if (lastid != 0) {// 首页
		sql += " and walletLogId<" + lastid;
	    }
	    sql += " order by walletLogId desc limit " + size;
	}
	return sql;
    }
    
    public String allPage(Map<String, Object> param) {
	String sql = "";
	if (param != null) {
	    int popid = (Integer) param.get("popid");
	    int size = (Integer) param.get("size");
	    sql += "select count(*)/" + size + "+1 "
		   + "from broker_wallet_log where 1=1 ";
	    sql += "and brokerId=" + popid;
	}
	return sql;
    }
    
    public String withdrawDetails(Map<String, Object> param) {
	String sql = "select walletLogId id,changeDesc,changeTime,purchaseAmount+tipAmount total "
		     + "from broker_wallet_log where 1=1 ";
	if (param != null) {
	    int popid = (Integer) param.get("popid");
	    int size = (Integer) param.get("size");
	    int lastid = (Integer) param.get("lastId");
	    sql += "and brokerId=" + popid
		   + " and (purchaseAmount<0 or tipAmount<0)";
	    if (lastid != 0) {// 首页
		sql += " and walletLogId<" + lastid;
	    }
	    sql += " order by walletLogId desc limit " + size;
	}
	return sql;
    }
    
    public String withdrawPage(Map<String, Object> param) {
	String sql = "";
	if (param != null) {
	    int popid = (Integer) param.get("popid");
	    int size = (Integer) param.get("size");
	    sql += "select count(*)/" + size + "+1 "
		   + "from broker_wallet_log where 1=1 ";
	    sql += "and brokerId=" + popid
		   + " and (purchaseAmount<0 or tipAmount<0)";
	}
	return sql;
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public String batchUpdateWithdrawOrder(Map<String, Object> param){
    	String batchOrderNo=(String)param.get("batchOrderNo");
    	List<Long> ids=(List)param.get("ids");
    	StringBuffer sb = new StringBuffer();
    	sb.append(" update withdraw_order set batchOrderNo = ");
    	sb.append(batchOrderNo);
    	sb.append(" where withdrawOrderId in (");
    	for(Long id:ids){
    		sb.append(id);
    		sb.append(",");
    	}
    	sb.append("-1)");
    	return sb.toString();
    }
    
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public String batchUpdateAliWithdrawOrder(Map<String,Object> param){
    	Integer status =(Integer) param.get("status");
    	Calendar finishTime =(Calendar) param.get("finishTime");
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	Long batchOrderId =(Long) param.get("batchOrderId");
    	Map<Long,String> reasonMap=(Map<Long,String>)param.get("reasonMap");
    	List<Long> ids=(List)param.get("ids");
    	StringBuffer sb=new StringBuffer();
    	sb.append(" update withdraw_order set finishTime = '");
    	sb.append(sdf.format(finishTime.getTime()));
    	sb.append("' ,batchOrderId = ");
    	sb.append(batchOrderId);
    	sb.append(" ,reason = case withdrawOrderId ");
    	
    	for(Long id:ids){
    		sb.append(" when ");
        	sb.append(id);
        	sb.append(" then '");
        	sb.append("".equals(reasonMap.get(id))?"提现成功":reasonMap.get(id));
        	sb.append("' ");
    	}
    	sb.append(" END ");
    	
    	sb.append(" ,status = ");
    	sb.append(status);
    	sb.append(" where withdrawOrderId in (");
    	for(Long id:ids){
    		sb.append(id);
    		sb.append(",");
    	}
    	sb.append("-1)");
    	return sb.toString();
    }
    @SuppressWarnings({ "unchecked", "rawtypes" })
	public String batchDeductBrokerWallet(Map<String,Object> param){
    	Map<Long,BigDecimal> moneyMap=(Map<Long,BigDecimal>)param.get("moneyMap");
    	List<Long> ids=(List)param.get("ids");
    	StringBuffer sb=new StringBuffer();
    	sb.append(" update broker_wallet b set b.frozenMoney=b.frozenMoney- case b.popid ");
    	for(Long id:ids){
    	sb.append(" when ");
    	sb.append(id);
    	sb.append(" then ");
    	sb.append(moneyMap.get(id));
    	}
    	
    	sb.append(" END ");
    	sb.append(" where b.popid in (");
    	for(Long id:ids){
    		sb.append(id);
    		sb.append(",");
    	}
    	sb.append("-1)");
    	return sb.toString();
    }
    
    @SuppressWarnings("unchecked")
	public String queryBrokerWalletLogsByOrderNo(Map<String,Object> param){
    	List<String> orderNos=(List<String>)param.get("orderNos");
    	StringBuffer sb=new StringBuffer();
    	sb.append(" select * from broker_wallet_log l where l.orderNo in (");
    	for(String orderNo:orderNos){
    		sb.append("'"+orderNo+"'");
    		sb.append(",");
    	}
    	sb.append("'')");
    	return sb.toString();
    }
    
    @SuppressWarnings("unchecked")
	public String batchUpdateBrokerWallet(Map<String,Object> param){
    	Map<Long,BigDecimal[]> addMap=(Map<Long,BigDecimal[]>)param.get("addMap");
    	StringBuffer sb=new StringBuffer();
    	sb.append(" update broker_wallet w set w.frozenMoney = w.frozenMoney- case w.popid ");
    	for (Entry<Long, BigDecimal[]> entry : addMap.entrySet()) {
    		sb.append(" when ");
        	sb.append(entry.getKey());
        	sb.append(" then ");
        	BigDecimal[] money=entry.getValue();
        	sb.append(Math.abs(money[0].doubleValue())+Math.abs(money[1].doubleValue()));
    	}
    	sb.append(" END, ");
    	sb.append(" w.purchaseBalance=w.purchaseBalance+ case w.popid");
    	for (Entry<Long, BigDecimal[]> entry : addMap.entrySet()) {
    		sb.append(" when ");
        	sb.append(entry.getKey());
        	sb.append(" then ");
        	BigDecimal[] money=entry.getValue();
        	sb.append(Math.abs(money[0].doubleValue()));
    	}
    	sb.append(" END, ");
    	
    	sb.append(" w.tipBalance=w.tipBalance+ case w.popid");
    	for (Entry<Long, BigDecimal[]> entry : addMap.entrySet()) {
    		sb.append(" when ");
    		sb.append(entry.getKey());
    		sb.append(" then ");
    		BigDecimal[] money=entry.getValue();
    		sb.append(Math.abs(money[1].doubleValue()));
    	}
    	sb.append(" END ");
    	
    	sb.append(" where w.popid in (");
    	for (Entry<Long, BigDecimal[]> entry : addMap.entrySet()) {
    		sb.append(entry.getKey());
    	}
    	sb.append(" ,-1)");
    	return sb.toString();
    }
    
    @SuppressWarnings("unchecked")
	public String batchInsertBrokerWalletLog(Map<String,Object> param){
    	List<BrokerWalletLog> logs=(List<BrokerWalletLog>)param.get("logs");
    	StringBuffer sb=new StringBuffer();
    	sb.append(" insert into broker_wallet_log(changeDesc,orderId,changeTime,brokerId,purchaseAmount,tipAmount,orderNo) values ");
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	for(int i=0;i<logs.size();i++){
    		sb.append("(");
    		sb.append("'提现失败退款',");
    		sb.append(logs.get(i).getOrderId()+",");
    		sb.append("'"+sdf.format(Calendar.getInstance().getTime())+"',");
    		sb.append(logs.get(i).getBrokerId()+",");
    		sb.append(Math.abs(logs.get(i).getPurchaseAmount().doubleValue())+",");
    		sb.append(Math.abs(logs.get(i).getTipAmount().doubleValue())+",");
    		sb.append("'"+logs.get(i).getOrderNo()+"'");
    		if(i==logs.size()-1){
    			sb.append(")");
    		}else{
    		sb.append("),");	
    		}
    	}
    	return sb.toString();
    }
}
